package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Employee;
import com.itheima.common.R;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

@Resource
private EmployeeService employeeService;


//登录
    @PostMapping("/login")
    public R login(@RequestBody Employee employe ,HttpServletRequest request ) {

            String pwd  = employe.getPassword();
            pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());

        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper();
            qw.eq(Employee ::getUsername,employe.getUsername());
        Employee one = employeeService.getOne(qw);
            if (one == null) {
               return R.error("用户名不存在");
            }
            if ( !one.getPassword().equals(pwd) ) {
               return R.error("密码错误");
            }
            if (one.getStatus()==0){
                return R.error("账号被封禁");
            }
            request.getSession().setAttribute("employee",one.getId());
        System.out.println(one);
        return R.success(one);

    }

//退出
    @PostMapping("/logout")
    public R<String> loginout (HttpServletRequest request){

        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }



//新增
    @PostMapping
    public R add( @RequestBody Employee employee) {
        log.info("新增员工，员工信息：{}", employee.toString());
        /*
        * //0、检查数据，哪些是需要处理的
//0.1: 密码使用默认的123456 ， 需要加密 DigestUtils.md5DigestAsHex
//0.2： status 不需要处理，数据库默认是1
//0.3： create_time 和 update_time ,设置为当前系统时间  LocalDateTime.now()
//0.4： create_user 和 update_user 创建人和修改人，应该是当前登陆人，从session中获取
        * */
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
       employee.setUpdateTime(LocalDateTime.now());
//        获得当前登录用户的id
//        Employee id = (Employee) session.getAttribute("employee");
//        System.out.println(id+"........................................");
//       employee.setCreateUser(id.getId());
//       employee.setUpdateUser(id.getId());
//        1、调用业务层保存 employee数据
        employeeService.save(employee);
        return R.success("添加成功");
    }

//人员分页
    @GetMapping("/page")
    public R<Page> page(@RequestParam int page, int pageSize, String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);

    }


//    人员编辑
    @PutMapping
    public R<String> update  (@RequestBody  Employee employee ){

//        Long id =(Long) session.getAttribute("employee");
//        employee.setUpdateUser(id.getId());
//            employee.setUpdateTime(LocalDateTime.now());
        boolean b = employeeService.updateById(employee);

        return b? R.success("修改成功"):R.error("失败");
    }


//员工的禁用和启用
    @GetMapping("/{id}")
    public R updateById (@PathVariable Employee employee ){

        boolean b = employeeService.updateById(employee);

        return b ? R.success("禁用成功"):R.error("禁用失败");
    }










}
